import http from '@/utils/request';

export interface GiftRecord {
    id?: string;
    name: string;
    amount: string | number;
    date: string;
    type: string;
    remarks?: string;
    bookId: string;
    books?: {
        id: string;
        title: string;
        type: string;
        coverImageUrl: string;
        userId: string;
        guestCount?: number;
        totalAmount?: number;
    };
}

export interface PageParams {
    pageNum?: number;
    pageSize?: number;
    name?: string;
    bookId: string;
    params?: Record<string, any>;
}

export interface PageResult<T> {
    msg: string;
    code: number;
    data: {
        records: T[];
        pageNumber: number;
        pageSize: number;
        totalPage: number;
        totalRow: number;
    };
}

// 获取礼簿记录列表
export function getGiftRecordsList(params: PageParams) {
    return http.post<PageResult<GiftRecord>>('/system/giftRecords/selectPage', {
        ...params,
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        params: {}
    });
}

// 添加礼簿记录
export function addGiftRecord(data: GiftRecord) {
    return http.post<any>('/system/giftRecords', data);
}

// 更新礼簿记录
export function updateGiftRecord(data: GiftRecord) {
    return http.put<any>('/system/giftRecords', data);
}

// 删除礼簿记录
export function deleteGiftRecord(id: string) {
    return http.delete<any>(`/system/giftRecords/${id}`);
}

// 获取礼簿记录详情
export function getGiftRecordDetail(id: string) {
    return http.get<any>(`/system/giftRecords/${id}`);
}

// 获取礼簿统计数据
export function getGiftRecordsDashboard(id: string) {
    return http.get<any>(`/system/giftRecords/dashboard/${id}`);
}

// 根据名称搜索礼簿记录
export function getGiftRecordsByName(params: { name: string; pageNum?: number; pageSize?: number; bookId?: string }) {
    return http.post<PageResult<GiftRecord>>('/system/giftRecords/listByName', {
        name: params.name,
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        bookId: params.bookId
    });
}

// 导出礼簿记录数据
export function exportGiftRecords(bookId: string) {
    return http.post<ArrayBuffer>('/system/giftRecords/exportByBookId', {}, {
        params: { bookId },
        responseType: 'arraybuffer'
    });
}
